﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Data.Filtering;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;

namespace RemoteImaging.LicensePlate
{
    public partial class FormSuspectCarQuery : XtraForm
    {
        private readonly IMessageBoxService _messageBoxService;

        public FormSuspectCarQuery()
        {
            InitializeComponent();
            UpcatePageIndicator();
        }

        public FormSuspectCarQuery(IMessageBoxService messageBoxService)
            : this()
        {
            if (messageBoxService == null) throw new ArgumentNullException("messageBoxService");
            _messageBoxService = messageBoxService;
        }

        private void sbSearch_Click(object sender, EventArgs e)
        {
        }

        private void carGridView_RowClick(object sender, RowClickEventArgs e)
        {
            ShowDetail(e.RowHandle);
        }

        private void ShowDetail(int rowHandle)
        {
            //var row = carGridView.GetRow(rowHandle) as SuspectCarQueryInfo;
            //if (row != null)
            //{
            //    try
            //    {
            //        LicensePlate.Helper.UpdateSuspectCarAlerm(this.suspectCarControl1, row.AlermInfo);
            //    }
            //    catch (Exception)
            //    {
            //        _messageBoxService.ShowError("图片丢失或损坏！");
            //    }

            //}
        }

        private void carGridView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            this.ShowDetail(e.FocusedRowHandle);
        }


        private class SuspectCarQueryInfo
        {
            public SuspectCarQueryInfo(SuspeciousCarAlermInfo alermInfo)
            {
                AlermInfo = alermInfo;
            }

            public SuspeciousCarAlermInfo AlermInfo { get; private set; }

            public string LicenseNumber
            {
                get
                {
                    return AlermInfo.ReportedCarInfo.LicenseNumber;
                }
            }

            public DateTime CaptureTime
            {
                get { return AlermInfo.CapturedLicenseInfo.CaptureTime; }
            }

            public DateTime ProcessTime { get; set; }
            public ProcessBehavior ProcessBehavior { get; set; }
            public string ProcessBehaviorDescription { get { return ProcessBehavior.GetDescription(); } }
        }

        private void gridView2_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {

        }

        private void prevPage_Click(object sender, EventArgs e)
        {
            if (xpPageSelector1.CurrentPage > 0)
            {
                xpPageSelector1.CurrentPage--;
                UpcatePageIndicator();
            }
        }

        private void nextPage_Click(object sender, EventArgs e)
        {
            if (xpPageSelector1.CurrentPage < xpPageSelector1.PageCount - 1)
            {
                xpPageSelector1.CurrentPage++;
                UpcatePageIndicator();

            }
        }

        private void UpcatePageIndicator()
        {
            currentPage.Text = string.Format("{0}/{1}页", xpPageSelector1.CurrentPage + 1, xpPageSelector1.PageCount);
            prevPage.Enabled = xpPageSelector1.CurrentPage > 0;
            nextPage.Enabled = xpPageSelector1.CurrentPage < xpPageSelector1.PageCount - 1;
        }

        private void refreshButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            //string cstr = string.Format("CapturedLicenseInfo.LicensePlateNumber like '%{0}%'", lpnToLookup.Text);
            //var c = CriteriaOperator.Parse(cstr);
            //xpCollection1.Criteria = c;
            //xpCollection1.Reload();

            var ps = new DevExpress.Xpo.XPPageSelector(xpCollection1);
            ps.PageSize = xpPageSelector1.PageSize;
            gridControl2.DataSource = ps;
            xpPageSelector1 = ps;

            this.pictureEdit1.DataBindings.Clear();
            this.pictureEdit1.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this.xpPageSelector1, "CapturedLicenseInfo.ImageCopy", true));

            capLpn.DataBindings.Clear();
            capLpn.DataBindings.Add(new Binding("EditValue", xpPageSelector1, "CapturedLicenseInfo.LicensePlateNumber", true));

            capTime.DataBindings.Clear();
            capTime.DataBindings.Add(new Binding("EditValue", xpPageSelector1, "CapturedLicenseInfo.CaptureTime", true));

            repCarColor.DataBindings.Clear();
            repCarColor.DataBindings.Add(new Binding("EditValue", xpPageSelector1, "ReportedCarInfo.Color", true));

            repCarType.DataBindings.Clear();
            repCarType.DataBindings.Add(new Binding("EditValue", xpPageSelector1, "ReportedCarInfo.CarType", true));

            repCaseNo.DataBindings.Clear();
            repCaseNo.DataBindings.Add(new Binding("EditValue", xpPageSelector1, "ReportedCarInfo.CaseNumber", true));

            repCaseUrl.DataBindings.Clear();
            repCaseUrl.DataBindings.Add(new Binding("EditValue", xpPageSelector1, "ReportedCarInfo.Url", true));

            repCreationTime.DataBindings.Clear();
            repCreationTime.DataBindings.Add(new Binding("EditValue", xpPageSelector1, "ReportedCarInfo.CreationTime", true));

            repEngineNo.DataBindings.Clear();
            repEngineNo.DataBindings.Add(new Binding("EditValue", xpPageSelector1, "ReportedCarInfo.EngineNumber", true));

            repFrameNo.DataBindings.Clear();
            repFrameNo.DataBindings.Add(new Binding("EditValue", xpPageSelector1, "ReportedCarInfo.CarFrameNumber", true));

            repLpn.DataBindings.Clear();
            repLpn.DataBindings.Add(new Binding("EditValue", xpPageSelector1, "ReportedCarInfo.LicenseNumber", true));

            repUpdateTime.DataBindings.Clear();
            repUpdateTime.DataBindings.Add(new Binding("EditValue", xpPageSelector1, "ReportedCarInfo.UpdateTime", true));

            UpcatePageIndicator();

            Cursor = Cursors.Default;
        }

        private void lpnToLookup_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.refreshButton_Click(null, null);
            }
        }

        private void FormSuspectCarQuery_Shown(object sender, EventArgs e)
        {
            this.pictureEdit1.DataBindings.Clear();
            this.pictureEdit1.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this.xpPageSelector1, "CapturedLicenseInfo.ImageCopy", true));

        }


    }



}